Improve error message for missing example/bin
authorAlex Crichton <alex@alexcrichton.com>
Mon, 9 Feb 2015 16:28:36 +0000 (08:28 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 9 Feb 2015 16:28:36 +0000 (08:28 -0800)
Closes rust-lang/crates.io#130

src/cargo/ops/cargo_run.rs
tests/test_cargo_test.rs

index 0ef3986a9f1ff26364958cda266dde918fe19263..6344ce4fb51b700594f0387663c8a00d3f9516a2 100644 (file)
@@ -27,7 +27,16 @@ pub fn run(manifest_path: &Path,
             !a.get_profile().is_custom_build()
     });
     let bin = try!(bins.next().chain_error(|| {
-        human("a bin target must be available for `cargo run`")
+        match (name.as_ref(), &target_kind) {
+            (Some(name), &TargetKind::Bin) => {
+                human(format!("no bin target named `{}` to run", name))
+            }
+            (Some(name), &TargetKind::Example) => {
+                human(format!("no example target named `{}` to run", name))
+            }
+            (Some(_), &TargetKind::Lib(..)) => unreachable!(),
+            (None, _) => human("a bin target must be available for `cargo run`"),
+        }
     }));
     match bins.next() {
         Some(..) => return Err(
index e9279d8eb4cfdef8ffc802eac46c6dfa7753b79b..fba5d46d8b728b2a70746563134b2066e636ba59 100644 (file)
@@ -1332,3 +1332,23 @@ test!(bin_is_preserved {
                 execs().with_status(0));
     assert_that(&p.bin("foo"), existing_file());
 });
+
+test!(bad_example {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "");
+
+    assert_that(p.cargo_process("run").arg("--example").arg("foo"),
+                execs().with_status(101).with_stderr("\
+no example target named `foo` to run
+"));
+    assert_that(p.cargo_process("run").arg("--bin").arg("foo"),
+                execs().with_status(101).with_stderr("\
+no bin target named `foo` to run
+"));
+});